package com.yc.snacknet_vue.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.snacknet_vue.bean.AddrInfo;
import com.yc.snacknet_vue.bean.MemberInfo;
import com.yc.snacknet_vue.biz.IAddrrInfoBiz;
import com.yc.snacknet_vue.biz.Impl.AddrInfoBizImpl;
import com.yc.snacknet_vue.utils.RequestToObject;
import com.yc.snacknet_vue.utils.SessionKeys;

/**
 * 地址信息业务控制层
 * @author UNO
 * @date 2021年8月13日
 * Email 2759451734@qq.com
 */
@WebServlet("/addr/*")
public class AddrInfoController extends BaseServlet{

	private static final long serialVersionUID = 1L;

	//查询登录会员的收货地址
	protected void finds(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Object obj = request.getSession().getAttribute(SessionKeys.CURRENTMEMLOGINACCOUNT);
		if( null == obj) {
			this.send(response, 501, null); //说明未登录
			return;
		}
		
		MemberInfo mem = (MemberInfo)obj;
		
		IAddrrInfoBiz biz = new AddrInfoBizImpl();
		List<AddrInfo> list = biz.findByMno(mem.getMno());
		if( null == list || list.isEmpty()) {
			this.send(response, 500, null); //说明无收货地址
			return;
		}
		this.send(response, 200, list);
	}
	//添加收货地址
	protected void add(HttpServletRequest request, HttpServletResponse response) throws IOException {

		Object obj = request.getSession().getAttribute(SessionKeys.CURRENTMEMLOGINACCOUNT);
		if( null == obj) {
			this.send(response, 501, null); //说明未登录
			return;
		}
		
		MemberInfo mem = (MemberInfo)obj;
		
		IAddrrInfoBiz biz = new AddrInfoBizImpl();
		//获取前端收获地址信息
		AddrInfo addr = RequestToObject.getParamesToObject(AddrInfo.class, request);
		addr.setMno( mem.getMno()); //会员编号
		String ano = String.valueOf( new Date().getTime());
		addr.setAno(ano);
		System.out.println(addr);
		int result = biz.add(addr);
		if( result <= 0 ) {
			this.send(response, 500, null);
			return;
		}
		this.send(response, 200, null);  //返回地址编号
	}

}
